<template>
<view>
	<view class="header">业绩分成</view>
	<view class="header1">业绩总数:{{total}}</view>
	<view class="content" v-if="shareShow">
		<view class="content_input" v-for="(item,index) in shareList" :key="index">
			<view class="content_title">{{item.name}}</view>
			<input type="number" v-model="item.money"
			placeholder-class="placeholderClass" disabled/>
		</view>
	</view>
	<view class="content" v-else>
		<view class="content_input" v-for="(item,index) in arr" :key="index">
			<view class="content_title">{{item.name}}</view>
			<input type="number" v-model="item.money"
			placeholder-class="placeholderClass" placeholder="请输入分成金额"/>
		</view>
	</view>
	<button v-if="addShow" class="buttonClass" @click="add">提交</button>
</view>
</template>
<script>
import request from '@/util/request.js'
export default {
	data() {
		return {
			//oaRole 1:店长2:员工3:商户
			oaRole:0,
			type:0,
			arr:[],
			total:'',
			order_id:0,
			shareShow:false,
			shareList:[],
			addShow:true
		}
	},
	onLoad:function(e){
		this.order_id = e.id
		this.type = e.type
		this.total = e.pay
		uni.showLoading({
		    title: '加载中'  
		});
		this.getShareInfo(e.id)
		//this.getStaff();
		this.oaRole = uni.getStorageSync('oaRole')
	},
	methods: {
		getShareInfo:function(order_id){
			var data = {
				order_id:order_id,
				type:this.type
			}
			var that = this
			request({
				url: 'manage/share/info',
				method: 'get',
				data:data,
				success: (res) => {
					if(res.data.data.length > 0){
						uni.hideLoading();
						that.shareShow = true
						that.shareList = res.data.data
						that.addShow = false
					}else{
						that.getUser()
					}
				},
			})
		},
		getUser:function(){
			var data = {
				oa_id:uni.getStorageSync('oa_id')
			}
			var that = this
			request({
				url: 'manage/user/all',
				method: 'get',
				data:data,
				success: (res) => {
					uni.hideLoading();
					that.checkArr(res.data.data)
				},
			})
		},
		checkArr:function(data){
			for(var x in data){
				this.arr.push({
					uid: data[x]['uid'],
					money:'',
					name:data[x]['name']
				})
			}
		},
		add:function(){
			var money = 0
			for(var x in this.arr){
				money += this.arr[x]['money'] - 1 + 1
			}
			if(money != this.total){
				uni.showToast({
					title: '分成金额和总数不一致!!',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			this.addShow = false
			var data = {
				oa_id:uni.getStorageSync('oa_id'),
				order_id:this.order_id,
				arr:this.arr,
				type:this.type
			}
			var that = this
			request({
				url: 'manage/share/add',
				method: 'post',
				data:data,
				success: (res) => {
					if(res.data.code == 1){
						uni.showToast({
							title:'成功',
							icon: 'none',
							duration: 2000,
							success() {
								setTimeout(function() {
									uni.navigateBack({//返回
										delta: 1
									})
								}, 2000);
							}
						})
					}
				},
			})
		}
	},
}
</script>
<style>
.header{
	width:100%;
	height:80rpx;
	line-height: 80rpx;
	text-align: center;
	color:#FFFFFF;
	background:#009900;
}
.header1{
	width:100%;
	height:80rpx;
	line-height: 80rpx;
	text-align: center;
	border:1rpx dashed #E8E8E8;
}
.content{
	padding:20rpx 20rpx 120rpx 20rpx;
}
.content_input{
	width:100%;
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	height:60rpx;
	line-height: 60rpx;
	margin-top:15rpx;
}
.content_title{
	width:45%;
	text-align: center;
}
.content_input input{
	width:45%;
	border:1rpx dashed #E8E8E8;
	height:60rpx;
	line-height: 60rpx;
	text-align: center;
}
</style>